package com.example.test.stub;

import java.io.File;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;

import com.example.manager.ManagerClient;
import com.example.terminal.TerminalClient;
import com.example.type.StreamType;
import com.example.util.DataBuilder;

public class MockAndroidClient {

	public static void main(String[] args) throws Exception {
//		MRequestT4AVConnect();
//		MnTRequestIPandPort();
//		TSendVedio2M();
		TSendFile2M();
	}

	/**
	 * M请求T的音视频命令
	 * @throws Exception
	 */
	static void MRequestT4AVConnect() throws Exception {
		TerminalClient tClient = new TerminalClient();
		tClient.register();
		tClient.start();

		ManagerClient mClient = new ManagerClient();
		mClient.requestAVConnect(tClient.getTerminalId());
	}
	
	/**
	 * M和T分别向状态服务器请求ip和port
	 * @throws Exception
	 */
	static void MnTRequestIPandPort() throws Exception {
		ManagerClient mClient = new ManagerClient(1000 * 60 * 10);
		mClient.startCommandSocket();
		
		mClient.login("120114708131", "123456");
		
		TerminalClient tClient = new TerminalClient();
		tClient.register();
		tClient.start(2);
		
		// M通知状态服务器要连接T（状态服务器会通知T）
		mClient.requestAVConnect(tClient.getTerminalId());
		
		// M和T各自连接中转管理服务器，请求分配的转发服务器的IP和端口,中转管理服务器返回分配的IP和端口
		mClient.requestAVIPAndPort(tClient.getTerminalId());
		tClient.requestAVIPAndPort(mClient.getUserId());
		
		Thread.sleep(1000 * 60 * 10);
	}
	
	/**
	 * M向T发送视频
	 * @throws Exception
	 */
	static void TSendVedio2M() throws Exception {
		ManagerClient mClient = new ManagerClient(1000 * 60 * 10);
		mClient.startCommandSocket();
		
//		mClient.login("120114708131", "123456");
		mClient.login("120103153224", "123456");
		
		TerminalClient tClient = new TerminalClient();
		tClient.register();
		tClient.start(2);
		
		mClient.requestAVConnect(tClient.getTerminalId());
		mClient.requestAVIPAndPort(tClient.getTerminalId());
		
		tClient.requestAVIPAndPort(mClient.getUserId());
		
		mClient.acceptStream(StreamType.Vedio);
		
//		String file = MockAndroidClient.class.getClassLoader().getResource("files/16kb.png").getFile();
		String file = MockAndroidClient.class.getClassLoader().getResource("files/16kb.png").getFile();
		tClient.sendVedio(new File(file));
		
		Thread.sleep(1000 * 60 * 10);
	}
	
	/**
	 * M请求T的文件文件
	 * @throws Exception
	 */
	static void TSendFile2M() throws Exception {
		ManagerClient mClient = new ManagerClient(1000 * 60 * 10);
//		mClient.startCommandSocket();
		mClient.start();
		mClient.login("120114708131", "123456");
		
		TerminalClient tClient = new TerminalClient();
		tClient.register();
		tClient.start(2);
		
		mClient.requestAVConnect(tClient.getTerminalId());
		
		/* 发送文件flag和md5校验码 */
		String file = MockAndroidClient.class.getClassLoader().getResource("files/test.txt").getFile();
//		String file = MockAndroidClient.class.getClassLoader().getResource("files/16kb.png").getFile();
		String md5 = DataBuilder.buidMD5Checksum(new File(file));
		mClient.requestAVIPAndPort(tClient.getTerminalId(), StreamType.File);
		tClient.requestAVIPAndPort(mClient.getUserId(), md5);
		
		mClient.acceptFile();
		
		
//		String file = MockAndroidClient.class.getClassLoader().getResource("files/16.jpeg").getFile();
		tClient.sendFile(new File(file));
		
//		Thread.sleep(1000 * 60 * 10);
		
	}
	
}
